﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsCrawler.Common.Interfaces;
using NewsCrawler.Common.Models;
using HtmlAgilityPack;
using CrawlerManager;
using System.Net;
using System.Text.RegularExpressions;
using System.ServiceModel.Syndication;
using System.Xml.Linq;
using DynamicRest;
using System.Globalization;

namespace Chip
{
    public class MainCrawler : ICrawler
    {
        private const string MainSiteUrl = "http://servis.chip.com.tr/chiponline-makale.xml";

        public List<Article> Parse()
        {
            return GenerateArticles(MainSiteUrl);
        }

        private List<Article> GenerateArticles(string siteURL)
        {
            List<Article> articles = new List<Article>();

            XDocument xdoc = XDocument.Load(siteURL);
            var links = from lv1 in xdoc.Descendants("item")
                        select
                              new
                              {
                                  url = lv1.Element("link").Value,
                                  date = DateTime.ParseExact(lv1.Element("pubDate").Value,
                                            "ddd, d MMM yyyy HH:mm:ss UT",
                                            CultureInfo.InvariantCulture),
                                  title = lv1.Element("title").Value,
                                  desc = lv1.Element("description").Value,
                                  parser = new SiteCrawler()
                              };
            links = links.OrderByDescending(p => p.date).Take(10);

            foreach (var item in links)
            {
                articles.Add(new Article { Url = item.url, Title = item.title, Description = item.desc, Parser = item.parser});
            }

            return articles;
        }
    }
}
